package cn.harry.cabinet.view.adapter

import android.annotation.SuppressLint
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageButton
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import cn.harry.cabinet.R
import cn.harry.cabinet.model.entity.Card
import cn.harry.cabinet.utils.DateConverter

/**
 * 卡片列表适配器
 */
class CardAdapter(
    private val onEditClick: (Card) -> Unit,
    private val onDeleteClick: (Card) -> Unit
) : RecyclerView.Adapter<CardAdapter.CardViewHolder>() {

    private var cards: List<Card> = emptyList()

    @SuppressLint("NotifyDataSetChanged")
    fun setCards(newCards: List<Card>) {
        cards = newCards
        notifyDataSetChanged()
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CardViewHolder {
        val view = LayoutInflater.from(parent.context)
            .inflate(R.layout.item_card, parent, false)
        return CardViewHolder(view)
    }

    override fun onBindViewHolder(holder: CardViewHolder, position: Int) {
        holder.bind(cards[position])
    }

    override fun getItemCount(): Int = cards.size

    inner class CardViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {

        fun bind(card: Card) {
            // 设置卡片基本信息
            itemView.findViewById<TextView>(R.id.cardNumber).text = card.cardNumber
            
            // 第一行：名称、类型、状态
            itemView.findViewById<TextView>(R.id.cardName).text = "名称:${card.name}"
            itemView.findViewById<TextView>(R.id.cardType).text = "类型:${card.type}"
            
            // 优化状态显示，使用不同颜色和中文
            val statusView = itemView.findViewById<TextView>(R.id.cardStatus)
            val statusText = when (card.status) {
                Card.STATUS_ENABLED -> "启用"
                Card.STATUS_DISABLED -> "禁用"
                else -> card.status
            }
            statusView.text = statusText
            
            // 使用硬编码颜色值避免API兼容性问题
            when (card.status) {
                Card.STATUS_ENABLED -> statusView.setTextColor(0xFF4CAF50.toInt()) // 绿色
                Card.STATUS_DISABLED -> statusView.setTextColor(0xFFF44336.toInt()) // 红色
                else -> statusView.setTextColor(0xFF9E9E9E.toInt()) // 灰色
            }
            
            // 第二行：备注、创建时间
            val remarkView = itemView.findViewById<TextView>(R.id.cardRemark)
            if (card.remark.isNotBlank()) {
                remarkView.text = card.remark
                remarkView.visibility = View.VISIBLE
            } else {
                remarkView.text = "无备注"
                remarkView.visibility = View.VISIBLE
            }
            
            itemView.findViewById<TextView>(R.id.cardCreateTime).text = 
                DateConverter.Companion.dateToString(card.createTime)

            // 设置图标按钮点击事件
            val editButton = itemView.findViewById<ImageButton>(R.id.editButton)
            val deleteButton = itemView.findViewById<ImageButton>(R.id.deleteButton)
            
            editButton.setOnClickListener { onEditClick(card) }
            deleteButton.setOnClickListener { onDeleteClick(card) }
        }
    }
}
